<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>订单详情</title>
    <link rel="stylesheet" href="/element-ui/index.css">
    <link rel="stylesheet" href="/element-ui/fonts/element-icons.css">
    <script src="/element-ui/vue.js"></script>
    <script src="/element-ui/index.js"></script>
    <script src="/element-ui/axios.min.js"></script>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '\\5FAE\\8F6F\\96C5\\9ED1', Arial, sans-serif;
            background-color: #f5f6fa;
        }
        #app {
            padding: 20px;
            background-color: #f5f6fa;
        }
        .page-title-bar {
            font-size: 26px;
            font-weight: bold;
            color: #333;
            background: #fff;
            padding: 24px 20px 12px 20px;
            border-bottom: 1px solid #eee;
            margin-bottom: 20px;
            letter-spacing: 2px;
            text-align: center;
            animation: fadeInDown 0.8s ease-out;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            border-radius: 8px;
        }
        @keyframes fadeInDown {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .el-main {
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        .el-button {
            transition: all 0.3s ease;
        }
        .el-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(0,0,0,0.1);
        }
        .el-button--primary:hover,
        .el-button--danger:hover,
        .el-button--info:hover {
            box-shadow: 0 4px 10px rgba(0,0,0,0.1);
        }
        .box-card {
            margin-bottom: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            animation: fadeIn 1s ease-out;
        }
        .box-card .el-card__header {
            font-weight: bold;
            font-size: 18px;
            color: #333;
        }
        .el-descriptions-item__label {
            font-weight: bold;
            color: #606266;
        }
        .el-descriptions-item__content {
            color: #303133;
        }
        .el-descriptions {
            animation: fadeIn 1s ease-out;
        }
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
        .el-table {
            animation: fadeIn 1s ease-out;
        }
        .el-table__row {
            transition: all 0.3s ease;
        }
        .el-table__row:hover {
            background-color: #f5f7fa !important;
            transform: scale(1.005);
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        .order-total {
            margin-top: 20px;
            text-align: right;
            font-size: 16px;
        }
        .total-amount {
            color: #f56c6c;
            font-weight: bold;
            font-size: 20px;
            margin-left: 10px;
        }
        .operation-buttons {
            margin-top: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="page-title-bar">订单详情</div>
    <div id="app">
        <el-container>
            <!-- <el-header>
                <h2>订单详情</h2>
            </el-header> -->
            <el-main>
                <!-- 订单基本信息 -->
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>订单信息</span>
                        <el-button style="float: right; padding: 3px 0" type="text" @click="goBack">返回</el-button>
                    </div>
                    <el-descriptions :column="2" border>
                        <el-descriptions-item label="订单编号">{{orderInfo.orderNo}}</el-descriptions-item>
                        <el-descriptions-item label="订单状态">
                            <el-tag :type="getStatusType(orderInfo.status)">{{getStatusText(orderInfo.status)}}</el-tag>
                        </el-descriptions-item>
                        <el-descriptions-item label="订购人">{{orderInfo.user.realName}}</el-descriptions-item>
                        <el-descriptions-item label="院系">{{orderInfo.user.department}}</el-descriptions-item>
                        <el-descriptions-item label="联系电话">{{orderInfo.user.phone}}</el-descriptions-item>
                        <el-descriptions-item label="创建时间">{{orderInfo.createTime}}</el-descriptions-item>
                    </el-descriptions>
                </el-card>

                <!-- 订单明细 -->
                <el-card class="box-card" style="margin-top: 20px;">
                    <div slot="header" class="clearfix">
                        <span>订单明细</span>
                    </div>
                    <el-table :data="orderDetails" border style="width: 100%" v-loading="loading">
                        <el-table-column prop="book.isbn" label="ISBN" width="180"></el-table-column>
                        <el-table-column prop="book.name" label="教材名称"></el-table-column>
                        <el-table-column prop="book.author" label="作者"></el-table-column>
                        <el-table-column prop="book.publisher.name" label="出版社"></el-table-column>
                        <el-table-column prop="quantity" label="数量" width="100"></el-table-column>
                        <el-table-column prop="price" label="单价" width="100"></el-table-column>
                        <el-table-column label="小计" width="120">
                            <template slot-scope="scope">
                                {{(scope.row.price * scope.row.quantity).toFixed(2)}}
                            </template>
                        </el-table-column>
                    </el-table>

                    <!-- 订单总计 -->
                    <div class="order-total">
                        <span>订单总计：</span>
                        <span class="total-amount">￥{{orderInfo.totalAmount}}</span>
                    </div>
                </el-card>

                <!-- 操作按钮 -->
                <div class="operation-buttons" v-if="orderInfo.status === 'PENDING'">
                    <el-button type="primary" @click="handlePay">确认付款</el-button>
                    <el-button type="danger" @click="handleCancel">取消订单</el-button>
                </div>
            </el-main>
        </el-container>
    </div>

    <script th:inline="javascript">
        new Vue({
            el: '#app',
            data() {
                return {
                    orderInfo: /*[[${orderInfo}]]*/ { user: {} },
                    orderDetails: /*[[${orderDetails}]]*/ [],
                    loading: false // Added loading state for table
                }
            },
            created() {
                // 移除AJAX数据获取逻辑，数据已由Thymeleaf直接注入
            },
            methods: {
                goBack() {
                    window.location.href = '/order/list';
                },
                handlePay() {
                    this.$confirm('确认将该订单标记为已付款?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.post('/order/pay', {
                            id: this.orderInfo.id
                        })
                        .then(response => {
                            if (response.data === 'success') {
                                this.$message.success('操作成功');
                                window.location.reload();
                            } else {
                                this.$message.error('操作失败');
                            }
                        })
                        .catch(error => {
                            console.error('Error updating status:', error);
                            this.$message.error('操作失败');
                        });
                    }).catch(() => {
                        this.$message.info('已取消操作');
                    });
                },
                handleCancel() {
                    this.$confirm('确认取消该订单?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.post('/order/updateStatus', {
                            id: this.orderInfo.id,
                            status: 'CANCELLED'
                        })
                        .then(response => {
                            if (response.data === 'success') {
                                this.$message.success('操作成功');
                                window.location.reload();
                            } else {
                                this.$message.error('操作失败');
                            }
                        })
                        .catch(error => {
                            console.error('Error cancelling order:', error);
                            this.$message.error('操作失败');
                        });
                    }).catch(() => {
                        this.$message.info('已取消操作');
                    });
                },
                getStatusType(status) {
                    switch (status) {
                        case 'PENDING':
                            return 'warning';
                        case 'PAID':
                            return 'success';
                        case 'CANCELLED':
                            return 'info';
                        default:
                            return '';
                    }
                },
                getStatusText(status) {
                    switch (status) {
                        case 'PENDING':
                            return '待付款';
                        case 'PAID':
                            return '已付款';
                        case 'CANCELLED':
                            return '已取消';
                        default:
                            return '';
                    }
                }
            }
        });
    </script>
</body>
</html> 